package compara;

import java.util.TreeSet;

public class ComparableDemo {
	public static void main(String[] args) {
		ComparableEntity comparableEntity1=new ComparableEntity();
		ComparableEntity comparableEntity2=new ComparableEntity();
		comparableEntity1.setI(1);
		comparableEntity1.setName("a");
		comparableEntity2.setI(1);
		comparableEntity2.setName("b");
		TreeSet<ComparableEntity> treeSet=new TreeSet<ComparableEntity>();
		treeSet.add(comparableEntity2);
		treeSet.add(comparableEntity1);
		for (ComparableEntity comparableEntity : treeSet) {
			System.out.println(comparableEntity);
		}
	}
}
class ComparableEntity implements Comparable<ComparableEntity>{
	private Integer i;
	private String name;

	public Integer getI() {
		return i;
	}

	public void setI(Integer i) {
		this.i = i;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int compareTo(ComparableEntity otherObject) {
		if(this.getI().compareTo(otherObject.getI())>0){
			return 1;
		}
		return this.getI().compareTo(otherObject.getI())==0?this.getName().compareTo(otherObject.getName()):-1;
	}
	@Override
	public String toString() {
		return "ComparableEntity [i=" + i + ", name=" + name + "]";
	}
	
}
